////////////////////////////////////////////////////////////////////////////////
//   Copyright GRGBanking Platform 2011 
//   All rights reserved. 
////////////////////////////////////////////////////////////////////////////////
package utils
{

	/*========================================================*/
	/*   Import                                               */
	/*========================================================*/

	import mx.collections.ArrayList;
	import mx.collections.IList;
	import mx.utils.ObjectUtil;

	public class ReflectUtil
	{

		/*========================================================*/
		/*   Constructor                                          */
		/*========================================================*/

		public function ReflectUtil()
		{
		}

		/*========================================================*/
		/*   Static Function                                      */
		/*========================================================*/

		public static function ObjectAddtoObject(source:Object, target:Object):Object
		{
			var objInfo:Object = mx.utils.ObjectUtil.getClassInfo(source);
			var fieldName:Array = objInfo["properties"] as Array;
			for each (var q:QName in fieldName)
			{
				//q.localName 属性名称，value对应的值
				target[q] = source[q.localName] as String;
					//var value:String = source[q.localName] as String;
			}
			return target;
		}

		public static function ObjectToArray(source:Object, filters:Array):ArrayList
		{
			var target:ArrayList = new ArrayList();
			var objInfo:Object = mx.utils.ObjectUtil.getClassInfo(source);
			var fieldName:Array = objInfo["properties"] as Array;
			var indexs:int = 0;
			var index1:int = 0;
			for each (var q:QName in fieldName)
			{
				var tempObject:Object = new Object();
				//q.localName 属性名称，value对应的值
				//target[q] = source[q.localName] as String;
				//var value:String = source[q.localName] as String;
				if (filters)
				{
					for each (var item:Object in filters)
					{
						if (q.localName == item.toString())
						{
							tempObject[item.toString()] = source[q.localName] as String;
							target.addItemAt(tempObject, index1);
							index1++;
						}
					}
				}
				else
				{
					tempObject[q.localName] = source[q.localName] as String;
					target.addItemAt(tempObject, indexs);
					indexs++;
				}
			}
			return target;

		}

		public static function XmlObjectToObject(source:Object):Object
		{
			var target:Object = new Object();
			var objInfo:Object = mx.utils.ObjectUtil.getClassInfo(source);
			var fieldName:Array = objInfo["properties"] as Array;
			for each (var q:QName in fieldName)
			{
				var attribute:String = q.localName.toString();
				target[attribute.replace("@", "")] = source[attribute].toString();
			}
			return target;
		}

		public static function addLanguageToArray(source:Object):ArrayList
		{
			var target:ArrayList = new ArrayList();
			var objInfo:Object = mx.utils.ObjectUtil.getClassInfo(source);
			var fieldName:Array = objInfo["properties"] as Array;
			var indexs:int = 0;
			for each (var q:QName in fieldName)
			{
//				var tempObject:Object = new Object();
//				tempObject["label"] = source[q.localName] as String;
//				//q.localName 属性名称，value对应的值
//				tempObject["value"] = q.localName;
				target.addItemAt(q.localName as Object, indexs);
				indexs++;
			}

			return target;
		}

		public static function addObjectToArray(source:Object):IList
		{
			var target:ArrayList = new ArrayList();
			var objInfo:Object = mx.utils.ObjectUtil.getClassInfo(source);
			var fieldName:Array = objInfo["properties"] as Array;
			var indexs:int = 0;
			for each (var q:QName in fieldName)
			{
				var tempObject:Object = new Object();
				tempObject["label"] = source[q.localName] as String;
				//q.localName 属性名称，value对应的值
				tempObject["value"] = q.localName;
				target.addItemAt(tempObject, indexs);
				indexs++;
			}
			return target;
		}

		public static function isObjectPropertiesValue(source:Object, expre:String):Boolean
		{
			var objInfo:Object = mx.utils.ObjectUtil.getClassInfo(source);
			var fieldName:Array = objInfo["properties"] as Array;
			for each (var q:QName in fieldName)
			{
				//q.localName 属性名称，value对应的值
				var value:String = source[q.localName] as String;
				if (StringUtil.stringsAreEqual(value, expre, true))
				{
					return true;
				}
					//var value:String = source[q.localName] as String;
			}
			return false;
		}
	} //endclass
} //endPackage